import guicomponents.*;


GPanel pnlControls;
GCombo cboX, cboY;
GOption optHand, optXhair, optMove, optText,optWait;
PFont plotFont;

 int selectedX = 0;
 int selectedY = 0;

int vitInterval = 100;
int podInterval = 100;
int corInterval = 100;
int poleInterval = 100;
int titInterval = 5;

PImage mapImage;
//Table locationTable;
int rowCount;

float plotX1, plotY1; 
float plotX2, plotY2; 

FloatTable dataTable; 
float dataMin = MAX_FLOAT;
float dataMax = MIN_FLOAT;
float dataMin2 = MAX_FLOAT;
float dataMax2 = MIN_FLOAT;
float dataMin3 = MAX_FLOAT;
float dataMax3 = MIN_FLOAT;
float dataMin4 = MAX_FLOAT;
float dataMax4 = MIN_FLOAT;
float dataMin5 = MAX_FLOAT;
float dataMax5 = MIN_FLOAT;
float percent = 0;


void setup() {
  size(700, 700);
  

  
  createCombos();
  //mapImage = loadImage("mapa3.png");
  //locationTable = new Table("locations.tsv");
  
  
  // Read the data table
  dataTable = new FloatTable("data.txt");
  
  rowCount = dataTable.getRowCount();
   // acho os valores maximos e minimos e uso eles em todos os casos
  
    for (int row = 0; row < rowCount; row++) {
    float value = dataTable.getFloat(row, 0);
    if (value > dataMax) {
      dataMax = value;
    }
    
      dataMin = 0;
    
  }
 
    dataMax = ceil(dataMax/ vitInterval) * vitInterval;
   
   
    for (int row = 0; row < rowCount; row++) {
   float value = dataTable.getFloat(row, 1);
    if (value > dataMax2) {
      dataMax2 = value;
    }
    dataMin2 = 0;
  }
  
  
  dataMax2 = ceil(dataMax2/ podInterval) * podInterval;
   
  
    for (int row = 0; row < rowCount; row++) {
    float value = dataTable.getFloat(row, 2);
    if (value > dataMax3) {
      dataMax3 = value;
    }
  dataMin3 = 0;
  }
  
  dataMax3 = ceil(dataMax3/ corInterval) * corInterval;
  
      for (int row = 0; row < rowCount; row++) {
    float value = dataTable.getFloat(row, 3);
    if (value > dataMax4) {
      dataMax4 = value;
    }
  dataMin4 = 0;
  }
  
  dataMax4 = ceil(dataMax4/ poleInterval) * poleInterval;
   
         for (int row = 0; row < rowCount; row++) {
    float value = dataTable.getFloat(row, 4);
    if (value > dataMax5) {
      dataMax5 = value;
    }
  dataMin5 = 0;
  }
  
  dataMax5 = ceil(dataMax5/ titInterval) * titInterval;
   
  plotX1 = 100; 
  plotX2 = width - plotX1; 
  plotY1 = 100; 
  plotY2 = height - plotY1; 
  
   plotFont = createFont("SansSerif", 20);
  textFont(plotFont);
  
 smooth();
 
 
}


void draw() {
  background(224);
  
   // Show the plot area as a white box 
  fill(255); 
  rectMode(CORNERS); 
  noStroke(); 
  rect(plotX1, plotY1, plotX2, plotY2); 
   
  handleComboEvents(cboX, cboY);
  drawTitle();
 
}

void drawTitle() {
  fill(0);
  textSize(20);
  textAlign(LEFT);
  String title = "Informações da História dos Países na Fórmula 1";
  text(title, width/3 - 90, plotY1 - 50);
}



int vitIntervalMinor = 50; 

//Número de vitórias
void drawVitY() {
 fill(0);
  textSize(10);
  textAlign(RIGHT);
  
   stroke(128);
  strokeWeight(1);

  for (float v = dataMin; v <= dataMax; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {    
      float y = map(v, dataMin, dataMax, plotY2, plotY1);  
      if (v % podInterval == 0) {        
        float textOffset = textAscent()/2;  
        if (v == dataMin) {
          textOffset = 0; 
        } else if (v == dataMax) {
          textOffset = textAscent();        
        }
        
         text(floor(v), plotX1 - 10, y + textOffset);
         line(plotX1 - 4, y, plotX1, y);
         } else {
        line(plotX1 - 2, y, plotX1, y);     // Draw minor tick
      }
        
        
      } 
    }
  
}


 

void drawVitX() {
 fill(0);
  textSize(10);
  textAlign(CENTER);
  
   stroke(224);
  strokeWeight(1);

  for (float v = dataMin; v <= dataMax; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {     
      float x = map(v, dataMin, dataMax, plotX1, plotX2);  
      if (v % podInterval == 0) {        
        float textOffset = textAscent()/2;  
        if (v == dataMin) {
          textOffset = 0;                   
        } else if (v == dataMax) {
          textOffset = textAscent();       
        }
        text(floor(v), x,  plotY2 + textAscent() + 10);
        line(x, plotY1, x, plotY2);
        
      } 
    }
  }
}

//Número de poles
void drawPoleY() {
 fill(0);
  textSize(10);
  textAlign(RIGHT);
  
   stroke(128);
  strokeWeight(1);

  for (float v = dataMin4; v <= dataMax4; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {    
      float y = map(v, dataMin4, dataMax4, plotY2, plotY1);  
      if (v % podInterval == 0) {        
        float textOffset = textAscent()/2;  
        if (v == dataMin4) {
          textOffset = 0; 
        } else if (v == dataMax4) {
          textOffset = textAscent();        
        }
        
         text(floor(v), plotX1 - 10, y + textOffset);
         line(plotX1 - 4, y, plotX1, y);
         } else {
        line(plotX1 - 2, y, plotX1, y);     // Draw minor tick
      }
        
        
      } 
    }
  
}


 

void drawPoleX() {
 fill(0);
  textSize(10);
  textAlign(CENTER);
  
   stroke(224);
  strokeWeight(1);

  for (float v = dataMin4; v <= dataMax4; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {     
      float x = map(v, dataMin4, dataMax4, plotX1, plotX2);  
      if (v % podInterval == 0) {        
        float textOffset = textAscent()/2;  
        if (v == dataMin4) {
          textOffset = 0;                   
        } else if (v == dataMax4) {
          textOffset = textAscent();       
        }
        text(floor(v), x,  plotY2 + textAscent() + 10);
        line(x, plotY1, x, plotY2);
        
      } 
    }
  }
}

//Número de titulos
void drawChampsY() {
 fill(0);
  textSize(10);
  textAlign(RIGHT);
  
   stroke(128);
  strokeWeight(1);

  for (float v = dataMin5; v <= dataMax5; v += 2.5) {
    if (v % 2.5 == 0) {    
      float y = map(v, dataMin5, dataMax5, plotY2, plotY1);  
      if (v % titInterval == 0) {        
        float textOffset = textAscent()/2;  
        if (v == dataMin5) {
          textOffset = 0; 
        } else if (v == dataMax5) {
          textOffset = textAscent();        
        }
        
         text(floor(v), plotX1 - 10, y + textOffset);
         line(plotX1 - 4, y, plotX1, y);
         } else {
        line(plotX1 - 2, y, plotX1, y);     // Draw minor tick
      }
        
        
      } 
    }
  
}


 

void drawChampsX() {
 fill(0);
  textSize(10);
  textAlign(CENTER);
  
   stroke(224);
  strokeWeight(1);

  for (float v = dataMin5; v <= dataMax5; v += 2.5) {
    if (v % 2.5 == 0) {     
      float x = map(v, dataMin5, dataMax5, plotX1, plotX2);  
      if (v % titInterval == 0) {        
        float textOffset = textAscent()/2;  
        if (v == dataMin5) {
          textOffset = 0;                   
        } else if (v == dataMax5) {
          textOffset = textAscent();       
        }
        text(floor(v), x,  plotY2 + textAscent() + 10);
        line(x, plotY1, x, plotY2);
        
      } 
    }
  }
}

//Número de podiuns
void drawPodY() {
 fill(0);
  textSize(10);
  textAlign(RIGHT);
  
   stroke(128);
  strokeWeight(1);

  for (float v = dataMin2; v <= dataMax2; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {     
      float y = map(v, dataMin2, dataMax2, plotY2, plotY1);  
      if (v % podInterval == 0) {       
        float textOffset = textAscent()/2;  
        if (v == dataMin2) {
          textOffset = 0;                  
        } else if (v == dataMax2) {
          textOffset = textAscent();       
        }
        
         text(floor(v), plotX1 - 10, y + textOffset);
        line(plotX1 - 4, y, plotX1, y);
         } else {
        line(plotX1 - 2, y, plotX1, y);     // Draw minor tick
      }
        
      
    }
  }
}


 

void drawPodX() {
 fill(0);
  textSize(10);
  textAlign(CENTER);
  
   stroke(224);
  strokeWeight(1);

  for (float v = dataMin2; v <= dataMax2; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {     
      float x = map(v, dataMin2, dataMax2, plotX1, plotX2);  
      if (v % podInterval == 0) {       
        float textOffset = textAscent()/2;  
        if (v == dataMin2) {
          textOffset = 0;                   
        } else if (v == dataMax2) {
          textOffset = textAscent();      
        }
        text(floor(v), x,  plotY2 + textAscent() + 10);
        line(x, plotY1, x, plotY2);
        
      } 
    }
  }
}




//Número de corridas
void drawCorY() {
 fill(0);
  textSize(10);
  textAlign(RIGHT);
  
   stroke(128);
  strokeWeight(1);

  for (float v = dataMin3; v <= dataMax3; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {     
      float y = map(v, dataMin3, dataMax3, plotY2, plotY1);  
      if (v % podInterval == 0) {       
        float textOffset = textAscent()/2;  
        if (v == dataMin3) {
          textOffset = 0;                  
        } else if (v == dataMax3) {
          textOffset = textAscent();        
        }
        
         text(floor(v), plotX1 - 10, y + textOffset);
        line(plotX1 - 4, y, plotX1, y);
         } else {
        line(plotX1 - 2, y, plotX1, y);     // Draw minor tick
      }
        
      
    }
  }
}


 

void drawCorX() {
 fill(0);
  textSize(10);
  textAlign(CENTER);
  
   stroke(224);
  strokeWeight(1);

  for (float v = dataMin3; v <= dataMax3; v += vitIntervalMinor) {
    if (v % vitIntervalMinor == 0) {     
      float x = map(v, dataMin3, dataMax3, plotX1, plotX2);  
      if (v % podInterval == 0) {       
        float textOffset = textAscent()/2;  
        if (v == dataMin3) {
          textOffset = 0;                   
        } else if (v == dataMax3) {
          textOffset = textAscent();        
        }
        text(floor(v), x,  plotY2 + textAscent() + 10);
        line(x, plotY1, x, plotY2);
        
      } 
    }
  }
}






// Draw the data as a series of points 
void drawDataPoints(int k,int j) { 
  float x=0;
  float y=0;
  int rowCount = dataTable.getRowCount(); 
  for (int row = 0; row < rowCount; row++) { 
    
      float value = dataTable.getFloat(row, k);
      float value2 = dataTable.getFloat(row, j);
      String country = dataTable.getRowName(row);
      
       if (k==0){
      x = map(value, dataMin, dataMax, plotY1, plotY2); 
  }
      else if (k==1){
        x = map(value, dataMin2, dataMax2, plotY1, plotY2); 
  }
        else if (k==2){
        x= map(value, dataMin3, dataMax3, plotY1, plotY2); 
  }
          else if (k==3){
        x= map(value, dataMin4, dataMax4, plotY1, plotY2); 
  }
          else if (k==4){
        x= map(value, dataMin5, dataMax5, plotY1, plotY2); 
  }
        if (j==0){
     y = map(value2, dataMin, dataMax, plotY2, plotY1);
  }
      else if (j==1){
        y = map(value2, dataMin2, dataMax2, plotY2, plotY1);
  }
        else if (j==2){
      y = map(value2, dataMin3, dataMax3, plotY2, plotY1);
  }
          else if (j==3){
      y = map(value2, dataMin4, dataMax4, plotY2, plotY1);
  }          else if (j==4){
      y = map(value2, dataMin5, dataMax5, plotY2, plotY1);
  }
  
  
      point(x, y); 
      
      if (dist(mouseX, mouseY, x, y) < 3) {
        strokeWeight(5);
        
        fill(0);
        textSize(10);
        textAlign(CENTER);
        text(round(value2)+","+round(value)+"("+country+")"  , x, y-10);
        textAlign(LEFT);
      }
    
  } 
} 





////funcao que cria os menus
public void createCombos() {
  String[] X = new String[] {
  "n° vitorias", "n° podiums" , "n° corridas", "n° poles", "n° titulos" };
  cboX = new GCombo(this, X, 5, width-100, height - 120, 100);
  cboX.setSelected(0);
  String[] Y = new String[] { 
    "n° vitorias", "n° podiums" , "n° corridas", "n° poles", "n° titulos"};
  cboY = new GCombo(this, Y, 5,width -  700, height - 620, 100);
  cboY.setSelected(0);
  

}
//
////funcao para comandar as acoes relacionadas a label

public void handleComboEvents(GCombo combo, GCombo combo2){
 
  
  if(cboX == combo && cboX.selectedIndex() == 0 ){
    drawVitX();
    selectedX = cboX.selectedIndex();
  }
  
   if(cboX == combo && cboX.selectedIndex() == 1 ){
    drawPodX();
    selectedX = cboX.selectedIndex();
  }
   if(cboX == combo && cboX.selectedIndex() == 2 ){
    drawCorX();
    selectedX = cboX.selectedIndex();
  }
     if(cboX == combo && cboX.selectedIndex() == 3 ){
    drawPoleX();
    selectedX = cboX.selectedIndex();
  }
       if(cboX == combo && cboX.selectedIndex() == 4 ){
    drawChampsX();
    selectedX = cboX.selectedIndex();
  }
  
   if(cboY == combo2 && cboY.selectedIndex() == 0 ){
    drawVitY();
    selectedY = cboY.selectedIndex();
  }
  
   if(cboY == combo2 && cboY.selectedIndex() == 1 ){
    drawPodY();
    selectedY = cboY.selectedIndex();
  }
   if(cboY == combo2 && cboY.selectedIndex() == 2 ){
    drawCorY();
    selectedY = cboY.selectedIndex();
  }
     if(cboY == combo2 && cboY.selectedIndex() == 3 ){
    drawPoleY();
    selectedY = cboY.selectedIndex();
  }
 
     if(cboY == combo2 && cboY.selectedIndex() == 4 ){
    drawChampsY();
    selectedY = cboY.selectedIndex();
  }
  
  strokeWeight(5); 
  stroke(#5679C1); 
  drawDataPoints(selectedX,selectedY); 
   
  }
 
 


 

 





